package com.google.code.sapwcrawler;

import com.google.code.sapwcrawler.download.cycle.DownloadCycle;
import com.google.code.sapwcrawler.robotstxt.RobotsTxtConfig;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Crawler implements Runnable
{
	static public final String version = "0.1";
	
	private URL startUrl;
	private DownloadCycle downloadCycle;
	private Logger logger;
	private CrawlerConfig config;
	private String host;
	public Crawler(URL startUrl,CrawlerConfig config)
	{
		this.startUrl 	= startUrl;
		this.host		= startUrl.getHost();
		this.logger		= Logger.getLogger(getHost());
		this.config		= config;
	}
	
	public Crawler(URL startUrl) throws MalformedURLException
	{
		this(startUrl,RobotsTxtConfig.readFromHost(startUrl.getHost()));
	}
	
	public void setConfig(CrawlerConfig cfg)
	{
		this.config = cfg;
	}
	
	public CrawlerConfig getConfig()
	{
		return this.config;
	}
	
	public Logger getLogger()
	{
		return logger;
	}
	
	public String getHost()
	{
		return this.host;
	}
	
	public void setDownloadCycle(DownloadCycle v)
	{
		this.downloadCycle = v;
	}
	
	public void run()
	{
		try
		{
			this.downloadCycle.addUrl(this.startUrl);
			this.downloadCycle.run();
		}
		catch(Throwable e)
		{
			logger.log(Level.SEVERE,e.toString());
		}
	}
}
